<?php
class Process extends Task {
	private $_tasks;
	private $_genreList;
	private $_genreLeaf;

	public function __construct($sqliteDB, $specFilename, $languageId = 25) {
		parent::__construct($sqliteDB, $specFilename, $languageId);
	}

	public function start($task, $type) {
		if ($task['active'] && $task['by_genre']) {
			$genreList = $this->validateGenre($type . '_genre', -1);
			$this->_genreList[$type] = $this->genreTraversal($genreList, $type, -1);
		}
	}

	public function getGenreList() {
		return $this->_genreList;
	}

	public function getGenreLeaf() {
		return $this->_genreLeaf;
	}

	public function buildContentList($task, $type, $genreId) {
		if (!$task['active'])
			return;

		if ($task['by_genre']) {
			$tmp = $this->validateContent($type, $genreId);
			$result = $tmp['result'];
			echo '<div id="' . $type . $genreId . '" class="noDisplay">';
			echo "\n\n";

			foreach ($result as $ret) {
				foreach ($ret as $cmsId => $item) {
					$this->mapSpec($type, $item);
				}
			}
			echo '</div>';

		}
		else { // all
		}

		if ($task['kid']) {
		}
	}

	private function mapSpec($type, $data) {
		//echo '<ul style="border:1px solid green;">';
		echo '<div class="contentItem">';
		foreach ($data as $fieldName => $value) {
			$tooltip = '';

			if ($value['status'] == 'KO') {
				$tooltip = '<abbr title="' . $value['message'] .'">' . $fieldName . '</abbr>';
			}
			else {
				$tooltip = $fieldName;
			}

			//echo '<li><b>' . $tooltip . '</b>: ' . $value['value'] . '</li>'; //need to check more detail
			//echo '<span><b>' . $tooltip . '</b>: ' . $value['value'] . '</span>'; //need to check more detail
			if ($fieldName == 'title') {
				echo '<span>' . $value['value'] . '</span>'; //need to check more detail
			}
		}
		echo '</div>';
	}

	/**
	 * traverse the genre tree.
	 */

	private function genreTraversal($genreList, $type, $parentId = -1) {
		$result = array();

		foreach($genreList['result'] as $genreId => $val) {
			$is_leaf = intval($val['is_leaf']['value']);

			if (-1 == $is_leaf) { // not a leaf, keep going
				$result[$genreId] = $val;
				$tmp = $this->validateGenre($type . '_genre', $genreId);
				$tmp2 = $this->genreTraversal($tmp, $type, $genreId);
				$result[$genreId]['leaf'] = $tmp2;
			}
			else {
				$total = $this->validateContent($type, $genreId); //total item for the genre
				$val['description']['value'] = $val['description']['value'] . ' (' . count($total['result']) . ')';
				$result[$genreId] = $val;
				$this->_genreList[$type][] = $genreId;
				$this->_genreLeaf[$type][] = $genreId;
			}
		}

		return $result;
	}

	public function buildGenreTree($genreList, $type) {
		echo '<ul>';
		foreach ($genreList as $genreId => $val) {
			$leaf = intval($val['is_leaf']['value']);
			$js = '';
			$tooltip = '';

			if ($val['description']['status'] != 'OK') {
				$tooltip = '<abbr title="' . $val['description']['message'] .'">' . $val['description']['value'] . '</abbr>';
			}
			else {
				$tooltip = $val['description']['value'];
			}

			if ($leaf == 1) {
				$js = ' onclick="showContent(\'' . $type . $genreId . '\');"';
			}
			echo '<li' . $js . '>' . $tooltip . '</li>';

			if ($leaf != 1) {
				$this->buildGenreTree($val['leaf'], $type);
			}
		}

		echo '</ul>';
	}
}
?>
